package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Constants;
import com.bjpowernode.crm.commons.domain.ResultEntity;
import com.bjpowernode.crm.settings.domain.DicValue;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.DicValueService;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.*;
import com.bjpowernode.crm.workbench.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

/**
 * 郭俊才
 * 2021/8/31
 */
@Controller
public class TranController {
    @Autowired
    private TranService tranService;
    @Autowired
    private UserService userService;
    @Autowired
    private DicValueService dicValueService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ContactsService contactsService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private TranHistoryService tranHistoryService;

    @RequestMapping("/workbench/transaction/index.do")
    public String transactionIndex(Model model){

        //从字典值中获取线索状态
        List<DicValue> stageList = dicValueService.queryByTypeCode("stage");
        //从字典值中获取线索来源
        List<DicValue> sourceList = dicValueService.queryByTypeCode("source");
        //从字典值中获取交易类型
        List<DicValue> transactionTypeList = dicValueService.queryByTypeCode("transactionType");
        model.addAttribute("transactionTypeList", transactionTypeList);
        model.addAttribute("stageList", stageList);
        model.addAttribute("sourceList", sourceList);

        return "workbench/transaction/index";
    }
    @RequestMapping("/workbench/transaction/createTran.do")
    public String createTran(Model model,HttpSession session){
        //获取用户信息
        List<User> users = userService.queryAllUser();
        List<User> userList = userService.queryAllUser();
        User user = (User) session.getAttribute(Constants.MESSAGE_SESSION_NAME);
        //移除掉当前用户的信息，当前用户的信息从session域中取
        for (int i = 0; i < users.size(); i++) {
            if (user.getId().equals(users.get(i).getId())) {
                users.remove(i);
                break;
            }
        }
        model.addAttribute("users", users);
        model.addAttribute("userList", userList);
        //从字典值中获取线索状态
        List<DicValue> stageList = dicValueService.queryByTypeCode("stage");
        //从字典值中获取线索来源
        List<DicValue> sourceList = dicValueService.queryByTypeCode("source");
        //从字典值中获取交易类型
        List<DicValue> transactionTypeList = dicValueService.queryByTypeCode("transactionType");
        model.addAttribute("transactionTypeList", transactionTypeList);
        model.addAttribute("stageList", stageList);
        model.addAttribute("sourceList", sourceList);

        return "workbench/transaction/save";
    }
    @RequestMapping("/workbench/tran/searchActivityByName.do")
    @ResponseBody
    public ResultEntity searchActivityByName(String activityName){
        List<Activity> activityList = activityService.queryByName(activityName);
        if(activityList != null && activityList.size() > 0){
            return ResultEntity.successWithData(activityList);
        }else {
            return ResultEntity.failed("查询失败");
        }
    }
    @RequestMapping("/workbench/tran/searchContactsByName.do")
    @ResponseBody
    public ResultEntity searchContactsByName(String fullName){
        List<Contacts> contactsList = contactsService.queryByName(fullName);
        if(contactsList != null && contactsList.size() > 0){
            return ResultEntity.successWithData(contactsList);
        }else {
            return ResultEntity.failed("查询失败");
        }
    }
    @RequestMapping("/workbench/transaction/queryCustomerByName.do")
    @ResponseBody
    public ResultEntity queryCustomerByName(String customerName){
        List<Customer> customerList = customerService.queryCustomerByName(customerName);
        if(customerList != null && customerList.size() > 0){
            return ResultEntity.successWithData(customerList);
        }else {
            return ResultEntity.failed("查询失败");
        }
    }
    @RequestMapping("/workbench/transaction/getPossibilityByStageValue.do")
    @ResponseBody
    public ResultEntity getPossibilityByStageValue(String stageValue){
        ResourceBundle bundle = ResourceBundle.getBundle("possibility");
        String possibility = bundle.getString(stageValue);
        return ResultEntity.successWithData(possibility);
    }
    @RequestMapping("/workbench/transaction/saveCreateTran.do")
    @ResponseBody
    public ResultEntity saveCreateTran(Tran tran,HttpSession session,String contactsName,String customerName){
        User user = (User) session.getAttribute(Constants.MESSAGE_SESSION_NAME);
        HashMap<String, Object> map = new HashMap<>();
        map.put("tran", tran);
        map.put("user", user);
        map.put("contactsName", contactsName);
        map.put("customerName", customerName);
        int i = tranService.saveSelective(map);
        if(i > 0){
            return ResultEntity.successWithoutData();
        }else {
            return ResultEntity.failed("保存失败");
        }
    }
    @RequestMapping("/workbench/transaction/detailTran.do")
    public String detailTran(String id,Model model){
        Tran tran = tranService.queryByPrimaryKeyForDetails(id);
        List<TranHistory> tranHistoryList = tranHistoryService.queryByTranPrimaryKeyForDetails(id);
        //从属性文件中读取可能性大小
        ResourceBundle bundle = ResourceBundle.getBundle("possibility");
        String possibility = bundle.getString(tran.getStage());
        //获取阶段历史信息
        List<DicValue> stageList = dicValueService.queryByTypeCode("stage");
        //获取阶段历史的最后一次成功的阶段
        TranHistory tranHistory = tranHistoryList.get(tranHistoryList.size() - 1);
        model.addAttribute("theOrderNo",tranHistory.getOrderNo());
        model.addAttribute("stageList",stageList);
        model.addAttribute("possibility",possibility);
        model.addAttribute("tran",tran);
        model.addAttribute("tranHistoryList",tranHistoryList);
        return "workbench/transaction/detail";
    }

}
